﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using Microsoft.Phone.Controls.Maps;
using Microsoft.Phone.Controls.Maps.Platform;

namespace TransportMe
{
    public class CostCalculator
    {
        private static double fuelCost = 2.1;//hardcoded

        public static double calculateCarbonEmissionsByDistance(Car c, double distance) 
        {
            double result = 0;
            result = c.CO2Combined * distance; //CO2Combined unit is gCO2/km
            return result; //result unit is gCO2 (grams of Carbon dioxide)
        }
        public static double calculateFuelConsumptionByDistance(Car c, double distance)
        {
            double result = 0;
            result = c.FuelConsumptionCombined * distance; //FuelConsumptionCombined unit is litres/km
            return result;
        }
        public static double calculateFuelCostByDistance(Car c, double distance)
        {
            double result = 0;
            result = calculateFuelCostByDistance(c, distance) * fuelCost;
            return result;
        }
        
        public static double calculateERPCost(LocationCollection listOfLocations, List<ERPRate> listOfERPRates)
        {
            double erpCost = 0;
            //total cost of erp that were passed by
            foreach(Location l in listOfLocations)
            {
                foreach(ERPRate er in listOfERPRates)
                {
                    if(l.Longitude == er.Longitude && l.Latitude == er.Latitude)
                    {
                        erpCost += er.ChargeAmount;
                    }
                }
                
            }
            return erpCost;
        }

        //public/ouble calculateTimeNeededForRoad(double roadDistance, TrafficCondition avgSpeed)
        //{
            
        //}

        //contains all expressway time taken on the route.
        //public double calculateEstimatedArrivalTimeForRoute(double expresswayTravelTime, double nonExpresswayTravelTime)
        //{

        //}

        public static CarPark calculateNearestCarPark(double longitude, double latitude)
        {
            //    XmlReader reader = XmlReader.Create("/CarPark.xml");
            //    XDocument doc = XDocument.Load(reader);

            double shortest = 99999999999999;
            CarPark nearestCarPark = null;
            foreach (CarPark cp in CarPark.listOfCarParks)
            {
                double relativeValueOfDistance = Math.Sqrt(((cp.Longitude - longitude) * (cp.Longitude - longitude)) + ((cp.Latitude - latitude) * (cp.Latitude - latitude)));
                if (relativeValueOfDistance < shortest)
                {
                    shortest = relativeValueOfDistance;
                    nearestCarPark = cp;
                }
            }

            return nearestCarPark;
        }

        public static double calculateCostOfParking(CarPark chosenCarPark, DateTime startParking)
        {
            double parkingCost = (chosenCarPark.EndTime.Minute - startParking.Minute) / 60 * chosenCarPark.Cost;
            return parkingCost;
        }

    }
}
